package com.ad.pet.common.data.http.interceptor;


import com.ad.pet.Logger;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ems on 2017/3/3.
 * 网络拦截器
 */
public class NetWorkInterceptor implements Interceptor {
    private static final String TAG = "NetWorkInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String s = request.url().toString();

        request = filter(s, request);
        request = checkServerAddress(request);
        Response response = chain.proceed(request);

        if (response == null) {
            Logger.e("response==null");
        }
//        //在别处登陆
//        if (response.code() == 403) {
//            Logger.d(TAG, "403", "url: ", s);
//            KickUtil.tip("登录信息已失效");
//        }
        return response;

    }

    private Request checkServerAddress(Request request) {
//        request.newBuilder().url(new HttpUrl.Builder().host("").build()).build();
//        HttpUrl build = request.url().newBuilder().host("").build();
//        String s = request.url().toString();
        return request;
    }

    /**
     * 过滤url 哪些要加token
     *
     * @param s
     * @param request
     * @return
     */
    private Request filter(String s, Request request) {
//        SpUtil sp = SpUtil.getInstance();
//        //获取短信跟创建用户不需要
//        if (!s.contains("common/sms/reg/") && !s.contains("common/sms/check/") && !s.contains("authority/login/")
//                && !s.contains("user/register") && (!s.contains("authority/password/") || request.method().equals("PUT"))) {
//            return request.newBuilder().addHeader("Authorization", "Token " + sp.getString(TokenKey.USER_ID)
//                    + "," + sp.getString(TokenKey.TOKEN)).build();
//        }

        return request;
    }
}
